package com.wydstation.power.properties.repository;

import com.wydstation.power.properties.dto.station.StationInfoDTO;
import com.wydstation.power.properties.util.UniqueIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.springframework.data.relational.core.query.Criteria.where;

@Repository
public class StationRepository {

    @Autowired
    private DatabaseClient databaseClient;


    public void insertBatch(List<StationInfoDTO> stationInfos) throws Exception {
        DatabaseClient.InsertIntoSpec insert = databaseClient
                .insert();
        for (StationInfoDTO station : stationInfos) {
            station.setStationId(setId());
//            station.getStationCollection().setStationId(station.getStationId());
            insert.into(StationInfoDTO.StationInfo.class).using(station.getStationInfo()).then().block();
            if (station.getStationClassify() != null) {
                station.getStationClassify().setStationId(station.getStationId());
                insert.into(StationInfoDTO.StationClassify.class)
                        .using(station.getStationClassify()).then().block();
            }
            if (station.getStationSpeedAttr() != null) {
                station.getStationSpeedAttr().setStationId(station.getStationId());
                insert.into(StationInfoDTO.StationSpeedAttr.class)
                        .using(station.getStationSpeedAttr()).then().block();
            }
            if (station.getStationGrid() != null) {
                station.getStationGrid().setStationId(station.getStationId());
                insert.into(StationInfoDTO.StationGrid.class)
                        .using(station.getStationGrid()).then().block();
            }
            if (station.getStationOther() != null) {
                station.getStationOther().setStationId(station.getStationId());
                insert.into(StationInfoDTO.StationOther.class)
                        .using(station.getStationOther()).then().block();
            }
        }
    }


    public void updateBatch(List<StationInfoDTO> stationInfos) {
        for (StationInfoDTO station : stationInfos) {
            databaseClient
                    .update()
                    .table(StationInfoDTO.StationInfo.class)
                    .using(station.getStationInfo())
                    .then()
                    .block();
            if (station.getStationClassify() != null) {
                station.getStationClassify().setStationId(station.getStationId());
                databaseClient
                        .update().table(StationInfoDTO.StationClassify.class)
                        .using(station.getStationClassify()).then().block();
            }
            if (station.getStationSpeedAttr() != null) {
                station.getStationSpeedAttr().setStationId(station.getStationId());
                databaseClient
                        .update().table(StationInfoDTO.StationSpeedAttr.class)
                        .using(station.getStationSpeedAttr()).then().block();
            }
            if (station.getStationGrid() != null) {
                station.getStationGrid().setStationId(station.getStationId());
                databaseClient
                        .update().table(StationInfoDTO.StationGrid.class)
                        .using(station.getStationGrid()).then().block();
            }
            if (station.getStationOther() != null) {
                station.getStationOther().setStationId(station.getStationId());
                databaseClient
                        .update().table(StationInfoDTO.StationOther.class)
                        .using(station.getStationOther()).then().block();
            }
        }
    }

    public void deleteStation(Class clazz, List<Long> ids) {
        ids.forEach(id -> {
            databaseClient
                    .delete()
                    .from(clazz)
                    .matching(where("station_id").is(id))
                    .then()
                    .block();
        });

    }

    private Long setId() throws Exception {
        return UniqueIDUtil.getUniqueID();
    }

}
